//
//  Font++.swift
//  YXRSmartDeviceDemo
//
//  Created by run on 2024/7/1.
//

import UIKit

extension UIColor {
    convenience init(_ R: CGFloat, _ G: CGFloat, _ B: CGFloat, _ A: CGFloat = 1) {
        self.init(red: R / 255.0, green: G / 255.0, blue: B / 255.0, alpha: A)
    }

    convenience init(hex: UInt32) {
        let mask = 0x0000_00FF

        let r = Int(hex >> 16) & mask
        let g = Int(hex >> 8) & mask
        let b = Int(hex) & mask

        let red = CGFloat(r) / 255.0
        let green = CGFloat(g) / 255.0
        let blue = CGFloat(b) / 255.0

        self.init(red: red, green: green, blue: blue, alpha: 1)
    }

    // UIColor -> Hex String
    var hexString: String? {
        var red: CGFloat = 0
        var green: CGFloat = 0
        var blue: CGFloat = 0
        var alpha: CGFloat = 0

        let multiplier = CGFloat(255.999999)

        guard getRed(&red, green: &green, blue: &blue, alpha: &alpha) else {
            return nil
        }

        if alpha == 1.0 {
            return String(
                format: "#FF%02lX%02lX%02lX",
                Int(red * multiplier),
                Int(green * multiplier),
                Int(blue * multiplier)
            )
        } else {
            return String(
                format: "#%02lX%02lX%02lX%02lX",
                Int(red * multiplier),
                Int(green * multiplier),
                Int(blue * multiplier),
                Int(alpha * multiplier)
            )
        }
    }

    static var random: UIColor {
        return UIColor(
            red: CGFloat(drand48()), green: CGFloat(drand48()), blue: CGFloat(drand48()), alpha: 1
        )
    }

    static var accessory: UIColor { return UIColor(198, 198, 204) }

    static var primary: UIColor { return .red }

    // 2 00AAAA
    static var secondary: UIColor { return UIColor(0, 170, 170) }

    // 3 191E23
    static var dark: UIColor { return UIColor(25, 30, 35) }

    // 4
    static var light: UIColor { return UIColor(90, 95, 110, 1) }

    // 5 191E23 60%
    static var lightest: UIColor { return UIColor(150, 157, 170, 1) }

    // 6 191E23 10%
    static var border: UIColor { return UIColor(220, 225, 235, 1) }

    // 7 F6F6F6
    static var background: UIColor { return UIColor(242, 244, 247, 1) }

    // 8
    static var tab: UIColor { return UIColor(185, 193, 207, 1) }

    // 9 F0322D
    static var important: UIColor { return UIColor(240, 50, 45, 1) }

    // 10
    // 11
    // 12 FF963C
    static var tagOrange: UIColor { return UIColor(255, 150, 60, 1) }
    // 13 #4187D2
    static var tagBlue: UIColor { return UIColor(65, 135, 210, 1) }
    // 14 191E23 30%
    static var tip: UIColor { return UIColor(25, 30, 35, 0.3) }
}

extension UIFont {
    static func numberFont(ofSize fontSize: CGFloat) -> UIFont {
        return UIFont(name: "DIN Alternate", size: fontSize) ?? UIFont.systemFont(ofSize: fontSize)
    }

    static let normal = UIFont.systemFont(ofSize: 14)
    static let normal16 = UIFont.systemFont(ofSize: 16)
    static let normal14 = UIFont.systemFont(ofSize: 14)
    static let small = UIFont.systemFont(ofSize: 12)
}

extension NSAttributedString {
    func heightWithConstrainedWidth(width: CGFloat) -> CGFloat {
        let constraintRect = CGSize(width: width, height: .greatestFiniteMagnitude)
        let boundingBox = boundingRect(
            with: constraintRect, options: .usesLineFragmentOrigin, context: nil
        )

        return boundingBox.height
    }

    func widthWithConstrainedHeight(height: CGFloat) -> CGFloat {
        let constraintRect = CGSize(width: .greatestFiniteMagnitude, height: height)
        let boundingBox = boundingRect(
            with: constraintRect, options: .usesLineFragmentOrigin, context: nil
        )

        return boundingBox.width
    }
}
